package top.fengleifeng.question;

import java.util.LinkedList;

/**
 * @author : fengleifeng
 * @Package top.fengleifeng.question
 * @Description:
 * @date Date : 2020年12月11日 5:08 下午
 */
public class Num1343大小为K且平均值大于等于阈值的子数组数目 {

    class Solution {
        public int numOfSubarrays(int[] arr, int k, int threshold) {
            int reuslt = 0;
            int endIndex = k ;
            int count = 0;
            LinkedList<Integer> linkedList = new LinkedList<>();
            for (int i = 0; i < k; i++) {
                linkedList.add(arr[i]);
                count += arr[i];
            }
            if ((count / k) >= threshold) {
                reuslt++;
            }
            while (endIndex < arr.length) {
                Integer first = linkedList.getFirst();
                count -= first;
                Integer now = arr[endIndex];
                count += now;
                linkedList.add(now);
                linkedList.removeFirst();
                if ((count / k) >= threshold) {
                    reuslt++;
                }
                endIndex++;
            }
            return reuslt;
        }
    }

    public static void main(String[] args) {
        int[] ints = {2, 2, 2, 2, 5, 5, 5, 8};
        int k = 3;
        int threshold = 4;
//        int[] ints = {1,1,1,1,1};
//        int k = 1;
//        int threshold = 0;
        int i = new Num1343大小为K且平均值大于等于阈值的子数组数目().new Solution().numOfSubarrays(ints, k, threshold);
        System.out.println(i);
    }
}
